package il.technion.cs236369.webserver;

import java.io.IOException;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


/**
 * This class stores the mappings for the MIME-type/type handlers
 * @author Nimrod
 *
 */
public class Mappings {

	public HashMap<String,String> extensionToClass = new HashMap<String,String>();
	public HashMap<String,String> TypeHandlers = new HashMap<String,String>();
	
	
	/**
	 * Parses the config file and fills webservers fields accordingly
	 * @throws ParserConfigurationException, SAXException, IOException, XPathExpressionException
	 * 
	 */
	
	public void parseHanlders() throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
		DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
		docFactory.setNamespaceAware(true);
		DocumentBuilder builder = docFactory.newDocumentBuilder();
		Document doc = builder.parse("config.xml");
		
		XPathFactory xpathFactory = XPathFactory.newInstance();
		XPath xpath = xpathFactory.newXPath();
		
		NodeList nl = (NodeList) xpath.compile("//type-handler").evaluate(doc, XPathConstants.NODESET);
		String handlerClass = nl.item(0).getAttributes().getNamedItem("class").getNodeValue();
		
		String jre = "";
		NodeList nl2 = (NodeList) xpath.compile("//parameter").evaluate(doc,XPathConstants.NODESET);
		int j = 0;
		while (!jre.equalsIgnoreCase("jre_path"))
		{
			jre = nl2.item(0).getAttributes().getNamedItem("name").getNodeValue();
			j++;
		}
		jre = nl2.item(0).getAttributes().getNamedItem("value").getNodeValue();
		System.setProperty("java.home", jre);
		
		for (int i=0; i < nl.getLength(); ++i) {
			String extension = xpath.compile("./extension[1]").evaluate(nl.item(i));
			String extension2 = xpath.compile("./extension[2]").evaluate(nl.item(i));
			TypeHandlers.put(extension, handlerClass);
			TypeHandlers.put(extension2, handlerClass);
		}
		nl = (NodeList) xpath.compile("//mime-mapping").evaluate(doc, XPathConstants.NODESET);
		for (int i=0; i < nl.getLength(); ++i) {
			String path = xpath.compile("./extension[1]").evaluate(nl.item(i));
			String extension = xpath.compile("./mime-type[1]").evaluate(nl.item(i));
			extensionToClass.put(path, extension);
		}
		
	}
	
	public String getMimeType(String type) {
		return extensionToClass.get(type);
	}
	
	public String getHandlerType(String type) {
		return TypeHandlers.get(type);
	}

	public String getContentType(ExtendedHttpRequest request){
		String type = "";
		String[] splittedPath = request.getPath().split("\\.");
		if (splittedPath.length > 1) {
			type = splittedPath[splittedPath.length-1];
		}
		
		//find mime type
		String contentType = getMimeType(type);
		if (contentType==null)
			contentType="";
		
		return contentType;
	}
	
	public static String getExtension(ExtendedHttpRequest request){
		String type = "";
		String[] splittedPath = request.getPath().split("\\.");
		if (splittedPath.length > 1) {
			type = splittedPath[splittedPath.length-1];
		}
		return type;
	}
	
}
